<?php
class Tag
{
	const LABEL_CHILD = 'child';
	const LABEL_TEXT = 'text';

	public $tagName;
	public $attributeNames;
	public $attributeValues;
	public $children;	//tag array
	public $texts;		//string array
	public $ordersOfContent;		//char array
	
	public function __construct($_tagName)
	{
		$this->tagName=$_tagName;
		$this->attributeNames=array();
		$this->attributeValues=array();
		$this->children=array();
		$this->texts=array();
		$this->ordersOfContent=array();
	}
	
	public function addAttribute($_attributeName,$_attributeValue)
	{
		$index=$this->findAttribute($_attributeName);
		if($index) {
			$this->attributeValues[$index]=(string)$_attributeValue;
			return;
		}
		$this->attributeNames[]=(string)$_attributeName;
		$this->attributeValues[]=(string)$_attributeValue;
	}
	
	public function getAttributeValue($_attributeName)
	{
		for($i=0;$i<count($this->attributeNames);$i++)
			if($this->attributeNames[$i]==$_attributeName)
				return $this->attributeValues[$i];
		return false;
	}
	
	public function setAttributeValue($_attributeName,$_attributeValue)
	{
		$index=$this->findAttribute($_attributeName);
		if(!$index) return false;
		$this->attributeValues[$index]=(string)$_attributeValue;
		return true;
	}

        public function setAttribute($_attributeName,$_attributeValue)
        {
            $this->setAttributeValue($_attributeName, $_attributeValue);
        }
	
	public function removeAttribute($_attributeName)
	{
		if(count($this->attributeNames)<=1) {
			$this->attributeNames=array();
			return true;
		}
		$index=$this->findAttribute($_attributeName);
		if(!$index) return false;
		$j=0;
		for($j=$index;$j<count($this->attributeNames)-1;$j++)
			$this->attributeNames[$j]=$this->attributeNames[$j+1];
		$this->attributeNames[$j]=null;
		return true;
	}
	
	public function findAttribute($_attributeName)
	{
		for($i=0;$i<count($this->attributeNames);$i++)
			if($this->attributeNames[$i]==$_attributeName)
				return $i;
		return false;
	}
	
	public function findChild($_childName)
	{
		for($i=0;$i<count($this->children);$i++)
			if($this->children[$i]->tagName==$_childName) 
				return $i;
		return false;
	}
	
	public function getChild($_childName)
	{
		$index=$this->findChild($_childName);
		return (!$index) ? false : $this->children[$index];
	}
	
	public function add($_value)
	{
		if(gettype($_value)=='string') {
			$this->ordersOfContent[]=self::LABEL_TEXT;
			$this->texts[]=$_value;
		} else {
			$this->ordersOfContent[]=self::LABEL_CHILD;
			$this->children[]=$_value;
		}
	}
	
	public function toString()
	{
		$string="<".$this->tagName;
		for($i=0;$i<count($this->attributeNames);$i++)
		{
			if(strlen(trim($this->attributeValues[$i]))>0) {
				$string.=" ".$this->attributeNames[$i]."=\"".str_replace("\"","'",$this->attributeValues[$i])."\"";
			}
		}
		if(count($this->children)>0) {
			$string.=">";
			$j=0;
			$k=0;
			while($j+$k<count($this->ordersOfContent))
			{
				if($this->ordersOfContent[$j+$k]==self::LABEL_TEXT) {
					if(strlen(trim($this->texts[$j]))>0) {
						$string.=$this->texts[$j];
					}
					$j++;
				} else {
					$childString=$this->children[$k]->toString();
					if(strlen(trim($childString))>0) {
						$string.=$childString;
					}
					$k++;
				}
			}
			$string.="</".$this->tagName.">";
		} else {
			$string.="/>";
		}
		return $string;
	}
}
?>